package com.lanxin.jet.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.lanxin.jet.beans.ContentBean;
import com.lanxin.jet.services.ContentService;
import com.lanxin.jet.services.ResourceService;
import com.lanxin.jet.services.UserService;
import com.lanxin.jet.tools.JetResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/content")
public class ContentController implements ResourceService.ImageOpt {

    @Autowired
    private ContentService contentService;

    @Autowired
    private UserService userService;

    @Override
    public Map<String, Object> setCover(String id, String path) {
        JetResponse jr = new JetResponse();
        if (!id.isEmpty()) {
            ContentBean content = contentService.get(id);
            if (content != null) {
                content.setCover(path);
                contentService.update(content);
                jr.success();
            } else {
                jr.error(JetResponse.RES_NOTHAS, "实体对象不存在");
            }
        } else {
            jr.error(JetResponse.RES_PARAMF);
        }
        return jr.map();
    }

    /**
     * 新建栏目
     * @param fid       父栏目id
     * @param title     标题
     * @param response
     */
    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public Map<String, Object> add (String fid, String title, HttpSession session) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(session, UserService.ROLE_CONTENT)) {
            jr.success(contentService.insert(fid, title));
        } else {
            jr.error(JetResponse.RES_TOKENF);
        }
        return jr.map();
    }

    /**
     * 获取内栏目联菜单
     * @param id        栏目id
     * @param level     最大递归数量
     * @param response
     */
    @RequestMapping(value = "/getBreadcrumb.do", method = RequestMethod.POST)
    public Map<String, Object> getBreadcrumb(String id, int level) {
        JetResponse jr = new JetResponse();
        jr.success(contentService.getBreadcrumb(id, level));
        return jr.map();
    }

    /**
     * 统计栏目数量
     * @param fid
     * @param keyword
     * @param response
     */
    @RequestMapping(value = "/getCount.do", method = RequestMethod.POST)
    public Map<String, Object> getCount(String fid, String keyword) {
        JetResponse jr = new JetResponse();
        jr.success(contentService.count(fid, keyword));
        return jr.map();
    }

    /**
     * 获取内容详情
     * @param id
     * @param response
     */
    @RequestMapping(value = "/get.do/id/{id}", method = RequestMethod.GET)
    public Map<String, Object> get(@PathVariable String id) {
        JetResponse jr = new JetResponse();
        ContentBean content = contentService.get(id);
        if (content != null) {
            jr.success(content);
        } else {
            jr.error(JetResponse.RES_NOTHAS, "栏目内容不存在");
        }
        return jr.map();
    }

    /**
     * 获取简单内容列表
     * @param offset
     * @param limit
     * @param fid
     * @param keyword
     * @param response
     */
    @RequestMapping(value = "/getSimpleList.do", method = RequestMethod.POST)
    public Map<String, Object> getList(int offset, int limit, String fid, String keyword) {
        JetResponse jr = new JetResponse();
        jr.success(contentService.getSimpleList(offset, limit, fid, keyword));
        return jr.map();
    }

    /**
     * 修改栏目
     * @param request
     * @param response
     */
    @RequestMapping(value = "/updateInfo.do", method = RequestMethod.POST)
    public Map<String, Object> updateAll(HttpServletRequest request) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(request.getSession(), UserService.ROLE_CONTENT)) {
            String id = request.getParameter("id");
            if (!id.isEmpty()) {
                ContentBean content = contentService.get(id);
                if (content != null) {
                    content.setFid(request.getParameter("fid"));
                    content.setTitle(request.getParameter("title"));
                    content.setKeyword(request.getParameter("keyword"));
                    content.setSort(Integer.parseInt(request.getParameter("sort")));
                    content.setData(request.getParameter("data"));
                    contentService.update(content);
                    jr.success();
                } else {
                    jr.error(JetResponse.RES_NOTHAS);
                }
            } else {
                jr.error(JetResponse.RES_PARAMF);
            }
        } else {
            jr.error(JetResponse.RES_TOKENF);
        }
        return jr.map();
    }

    /**
     * 修改栏目内容
     * @param id
     * @param detail
     * @param response
     */
    @RequestMapping(value = "/saveDetail.do", method = RequestMethod.POST)
    public Map<String, Object> updateDetail(HttpSession session, String id, String detail) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(session, UserService.ROLE_CONTENT)) {
            if (!id.isEmpty()) {
                ContentBean content = contentService.get(id);
                if (content != null) {
                    content.setDetail(detail);
                    contentService.update(content);
                    jr.success();
                } else {
                    jr.error(JetResponse.RES_NOTHAS);
                }
            } else {
                jr.error(JetResponse.RES_PARAMF);
            }
        } else {
            jr.error(JetResponse.RES_TOKENF);
        }
        return jr.map();
    }

    /**
     * 设置栏目状态
     * @param id
     * @param status
     * @param response
     */
    @RequestMapping(value = "/setStatus.do", method = RequestMethod.POST)
    public Map<String, Object> setStatus(HttpSession session, String id, int status) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(session, UserService.ROLE_CONTENT)) {
            if (!id.isEmpty()) {
                ContentBean content = contentService.get(id);
                if (content != null) {
                    content.setStatus(status);
                    contentService.update(content);
                    jr.success();
                } else {
                    jr.error(JetResponse.RES_NOTHAS);
                }
            } else {
                jr.error(JetResponse.RES_PARAMF);
            }
        } else {
            jr.error(JetResponse.RES_TOKENF);
        }
        return jr.map();
    }

    /**
     * 删除栏目
     * @param id
     * @param response
     */
    @RequestMapping(value = "/delete.do", method = RequestMethod.POST)
    public Map<String, Object> delete(String id, HttpSession session) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(session, UserService.ROLE_CONTENT)) {
            if (contentService.count(id, "") == 0) {
                contentService.delete(id);
                jr.success();
            } else {
                jr.error(JetResponse.RES_NOCHAN, "安全起见，需要先删除子栏目");
            }
        } else {
            jr.error(JetResponse.RES_TOKENF);
        }
        return jr.map();
    }

}